
*************************
clear                                                                                                   
clear matrix    
clear mata                                                                                        
capture clear                                                                                           
capture log close                                                                                                                                                                       
set more off   
set maxvar 10000    
*************************

****************************************************************************
****************************************************************************
**      2012 and 2015 RURAL AGRICULTURAL LIVELIHOODS SURVEY (RALS)        **
**  Country: ZMB1215 							  **
**  Year: 2012/2015                                                       **                    
**  Program: maps1215.do    					          **
**  Program first written: May 27, 2016 (Federica Alfani)	      	  **
**  This version:          May 27, 2016 (Federica Alfani)		  **
**  Output: $OUT/maps1215.dta  						  **
****************************************************************************
****************************************************************************


******************************************************************************************
** This file maps WARD level maps using new ARC2 data prepared by Gianluca Franceschini **
******************************************************************************************

/* First convert the ward level shape files to stata files
shp2dta using Ward2010, database(wards) coordinates(wardcoor) genid(id) replace
NOTE: the above command is used only once to create a data file "wards.dta" with data on wards and 
another called "wardcoord.dta" with coordinates */

/* --- Upload file of interest  --- */
use "$TEMP12/Zambia1215.dta", clear
*use "$TEMP/ZMB1215.dta", clear


collapse aez gsimpson gsimpson_lv gsimpson_inc shlivestock_totinc shlivestock_totinc2 shagriculture_income ///
		 seasontot RainDev* seasind oseasind meanrain covrain  dagrof_hh , by(year dist)

		 decode   dist, g(dist2)

sort dist2
lab dat "RALS 2012 Policy district level data.dta"  
save  "$spatial\tempale", replace

use "$spatial\dist.dta", clear
ren ADM2_NAME dist2
sort dist2

merge 1:m dist2 using "$spatial\tempale"
drop if year==.

replace gsimpson               =round(gsimpson     , 0.01)  
replace gsimpson_lv            =round(gsimpson_lv  , 0.01)  
replace gsimpson_inc           =round(gsimpson_inc , 0.01)  
replace shlivestock_totinc     =round(shlivestock_totinc, 0.01)
replace shlivestock_totinc2    =round(shlivestock_totinc2, 0.01)
replace shagriculture_income   =round(shagriculture_income, 0.01)
replace seasontot 	       =round(seasontot) 
replace RainDev 	       =round(RainDev, 0.01) 
replace covrain 	       =round(covrain, 0.01) 
replace meanrain 	       =round(meanrain, 0.1) 
replace dagrof_hh  =round(dagrof_hh, 0.1) 
/*
*** GINI-SIMPSON VARIABLES  
preserve 
keep if year==2011
spmap gsimpson     using "$spatial\distcoor.dta" if year==2011, id(distid) fcolor(Greens) clnumber(4) legend(symy(*1) symx(*1) size(*1)) legorder(lohi) title(Crop) legend(pos(4)) saving("$spatial\gsimpson11", replace)
spmap gsimpson_lv  using "$spatial\distcoor.dta" if year==2011, id(distid) fcolor(Oranges) clnumber(4) legend(symy(*1) symx(*1) size(*1)) legorder(lohi) title(Livestock) legend(pos(4)) saving("$spatial\gsimpson_lv11", replace)
spmap gsimpson_inc using "$spatial\distcoor.dta" if year==2011, id(distid) fcolor(Blues)   clnumber(4) legend(symy(*1) symx(*1) size(*1)) legorder(lohi) title(Income) legend(pos(4)) saving("$spatial\gsimpson_inc11", replace)
restore
graph combine "$spatial\gsimpson11" "$spatial\gsimpson_lv11" "$spatial\gsimpson_inc11", row(1) saving("$spatial\FigureGS12", replace) subtitle(Gini-Simpson Indices - RALS 2012)

preserve 
keep if year==2014
spmap gsimpson     using "$spatial\distcoor.dta" if year==2014, id(distid) fcolor(Greens) clnumber(4) legend(symy(*1) symx(*1) size(*1)) legorder(lohi) title(Crop) legend(pos(4))  saving("$spatial\gsimpson14", replace)
spmap gsimpson_lv  using "$spatial\distcoor.dta" if year==2014, id(distid) fcolor(Oranges) clnumber(4) legend(symy(*1) symx(*2) size(*1)) legorder(lohi) title(Livestock) legend(pos(4)) saving("$spatial\gsimpson_lv14", replace)
spmap gsimpson_inc using "$spatial\distcoor.dta" if year==2014, id(distid) fcolor(Blues)   clnumber(4) legend(symy(*1) symx(*2) size(*1)) legorder(lohi) title(Income) legend(pos(4)) saving("$spatial\gsimpson_inc14", replace)
restore
graph combine "$spatial\gsimpson14" "$spatial\gsimpson_lv14" "$spatial\gsimpson_inc14", row(1) saving("$spatial\FigureGS15", replace) subtitle(Gini-Simpson Indices - RALS 2015)
graph combine "$spatial\FigureGS12" "$spatial\FigureGS15", row(2) saving("$spatial\FigureGS", replace)


** Rainfall vars
preserve 
keep if year==2014
spmap meanrain using "$spatial\distcoor.dta", id(distid) fcolor(Greens)   clnumber(4) legend(symy(*1) symx(*1) size(*1)) legorder(lohi) title(Average rainfall (mm/season)) legend(pos(4)) saving("$spatial\meanrain", replace)
spmap covrain  using "$spatial\distcoor.dta", id(distid) fcolor(Oranges)   clnumber(4) legend(symy(*1) symx(*1) size(*1)) legorder(lohi) title(Coefficient of variation of rainfall) legend(pos(4)) saving("$spatial\covrain", replace)
restore
graph combine "$spatial\meanrain" "$spatial\covrain", row(1) saving("$spatial\Figurerain", replace)
*/
spmap dagrof_hh if year==2011 using "$spatial\distcoor.dta", id(distid) fcolor(Purples)   clnumber(4) legend(symy(*1) symx(*1) size(*1)) legorder(lohi) legend(pos(4)) saving("$spatial\dagrof_hh11", replace)
spmap dagrof_hh if year==2014 using "$spatial\distcoor.dta", id(distid) fcolor(Purples)   clnumber(4) legend(symy(*1) symx(*1) size(*1)) legorder(lohi) legend(pos(4)) saving("$spatial\dagrof_hh14", replace)
graph combine "$spatial\dagrof_hh11" "$spatial\dagrof_hh14", row(1) saving("$spatial\dagrof_hh", replace)

** Poverty Measures **
use "$TEMP/ZMB1215.dta", clear

collapse pc_gross_income2_trim dpoor_cpi, by(year dist)

		 decode   dist, g(dist2)

sort dist2
lab dat "RALS 2012 Policy district level data.dta"  
save  "$spatial\tempale", replace

use "$spatial\dist.dta", clear
ren ADM2_NAME dist2
sort dist2

merge 1:m dist2 using "$spatial\tempale"
drop if year==.

replace  dpoor_cpi= dpoor_cpi*100
replace  dpoor_cpi  =round(dpoor_cpi, 0.1)  

preserve 
keep if year==2011
spmap dpoor_cpi  using "$spatial\distcoor.dta" if year==2011, id(distid) fcolor(Blues) /*clnumber(4) clmethod(custom) clbreaks(25.6 47.61 57.31 62.51 86.8) legend(symy(*1) symx(*1) size(*1.5)) legorder(lohi)*/ legenda(off)  title(RALS 2012) legend(pos(4)) saving("$spatial\FGT0_11", replace)
restore

preserve 
keep if year==2014
spmap dpoor_cpi     using "$spatial\distcoor.dta" if year==2014, id(distid) fcolor(Blues) clnumber(4) clmethod(custom) clbreaks(25.6 47.61 57.31 62.51 86.8) legend(symy(*1) symx(*1) size(*1.5)) legorder(lohi) title(RALS 2015) legend(pos(4))  saving("$spatial\FGT0_14", replace)
restore
graph combine "$spatial\FGT0_11" "$spatial\FGT0_14", row(1) saving("$spatial\Figure_FGT0", replace) subtitle("Poverty Headcount, % (FGT0)",  height(15))


spmap pc_gross_income2_trim if year==2011 using "$spatial\distcoor.dta", id(distid) fcolor(Greens) legenda(off)  title(RALS 2012) legend(pos(4)) saving("$spatial\INC_11", replace)
spmap pc_gross_income2_trim if year==2014 using "$spatial\distcoor.dta", id(distid) fcolor(Greens) clnumber(4) clmethod(custom) clbreaks(107 185 228 268 764)  legend(size(*1.5)) legorder(lohi) title(RALS 2015) legend(pos(4)) saving("$spatial\INC_14", replace)
graph combine "$spatial\INC_11" "$spatial\INC_14", row(1) saving("$spatial\Figure_INC", replace) subtitle("Income per capita, ZMK",  height(15))


exit
***** GENERATE MAPS - POLICY VARIBLES 
spmap fisp     using distcoor.dta           , id(distid) fcolor(Blues) clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(FISP (%) - RALS2012) legend(pos(4))
spmap fra      using distcoor.dta           , id(distid) fcolor(Reds) clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Nr of FRA deposits - RALS2012) legend(pos(4))
* spmap numbanks using distcoor.dta         , id(distid) fcolor(Greens) clnumber(5) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Nr of banks - RALS2012) legend(pos(4))spmap cetzam   using distcoor.dta      , id(distid) fcolor(Oranges) clnumber(5) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(CETZAM (%) - RALS2012) legend(pos(4))
* spmap znfu_all using distcoor.dta         , id(distid) fcolor(Oranges) clnumber(5) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(ZNFU ALL - RALS2012) legend(pos(4))
* spmap cfu_all  using distcoor.dta         , id(distid) fcolor(Oranges) clnumber(5) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(CFU ALL (%) - RALS2012) legend(pos(4))
spmap ext_adv  using distcoor.dta           , id(distid) fcolor(Oranges) clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Extension advice (%) - RALS2012) legend(pos(4))
spmap loan     using distcoor.dta           , id(distid) fcolor(Greens)  clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Access to credit (%) - RALS2012) legend(pos(4))
***** GENERATE MAPS - OUTCOME INDICATORS 
spmap pc_gross_income2  using distcoor.dta   , id(distid) fcolor(Greens) clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Monthly per capita income (USD) - RALS2012) legend(pos(4))
spmap fgt0              using distcoor.dta   , id(distid) fcolor(Greys) clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Income poverty headcount (%) - RALS2012) legend(pos(4))

*** COUNT VARIABLES  
spmap n_crops    using distcoor.dta      , id(distid) fcolor(Oranges) clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Crop count - RALS2012) legend(pos(4))
spmap hldc       using distcoor.dta      , id(distid) fcolor(Greens)  clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Livestock count - RALS2012) legend(pos(4))
spmap count_inc  using distcoor.dta      , id(distid) fcolor(Blues)   clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Income sources count - RALS2012) legend(pos(4))


*** SHARES 
spmap shlivestock_totinc           using distcoor.dta      , id(distid) fcolor(Oranges) clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Livestock income / Total income (Share) - RALS2012) legend(pos(4))
spmap shlivestock_totinc2          using distcoor.dta      , id(distid) fcolor(Greens)  clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Livestock income / Agricultural income (Share) - RALS2012) legend(pos(4))
spmap shagriculture_income            using distcoor.dta      , id(distid) fcolor(Blues)   clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Agricultural income / Total income (Share) - RALS2012) legend(pos(4))

*** VULNERABILITY TO POVERTY MAP 
spmap vep_asset            using distcoor.dta      , id(distid) fcolor(Greys)   clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Vulnerability to poverty (%) - RALS2012) legend(pos(4))

** Rainfall vars
spmap meanrain    using "$spatial\distcoor.dta" , id(distid) fcolor(Greens)   clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Average rainfall 1983-2012 (mm/season)) legend(pos(4))
spmap covrain    using distcoor.dta , id(distid) fcolor(Oranges)   clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Coefficient of variation of rainfall 1983-2012) legend(pos(4))
spmap seasontot    using distcoor.dta , id(distid) fcolor(Blues)   clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Average rainfall during 2010/11 season (mm.)) legend(pos(4))

* BuYlRd and Rainbow color schemes give diff colors, but are inverted
spmap RainDev    using distcoor.dta , id(distid) fcolor(Reds)   clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(|Rainfall deviation| 2010/11 season (%)) legend(pos(4))
spmap RainDev2    using distcoor.dta , id(distid) fcolor(Blues)   clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Rainfall deviation 2010/11 season (%)) legend(pos(4))

spmap wealth_index0 using distcoor.dta      , id(distid) fcolor(Blues)   clnumber(4) legend(symy(*2) symx(*2) size(*2)) legorder(lohi) title(Wealth index (w/o lvsk) - RALS2012) legend(pos(4))







